import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class SolutionInterview17_5 {
    public String[] findLongestSubarray(String[] array) {
        int[] f=new int[array.length];
        for (int i=0;i<array.length;i++){
            if (isAlpha(array[i])){
                f[i]++;
            }else {
                f[i]--;
            }
        }
        Map<Integer,Integer> map=new HashMap<>();
        map.put(0,0);
        int max=0,start=-1,end=-1;
        int sum=0;
        for (int i=0;i<array.length;i++){
            sum+=f[i];
            if (!map.containsKey(sum)){
                map.put(sum,i+1);
            }else {
                if (i-map.get(sum)+1>max){
                    max=i-map.get(sum)+1;
                    start=map.get(sum);
                    end=i;
                }
            }
        }
        if (start==-1){
            return new String[]{};
        }
        String[] ans=new String[end-start+1];
        System.arraycopy(array,start,ans,0,end-start+1);
        return ans;
    }

    boolean isAlpha(String t){
        return t.charAt(0)>='A' && t.charAt(0)<='Z' || t.charAt(0)>='a' && t.charAt(0)<='z';
    }

    public static void main(String[] args) {
        System.out.println(Arrays.toString(new SolutionInterview17_5().findLongestSubarray(new String[]{"A","1","B","C","D","2","3","4","E","5","F","G","6","7","H","I","J","K","L","M"})));
    }
}
